setTimeout(function(){
	html = ttCollectPanel();
	btn = shrinkBtn('头条面板');
	$('html body').append(btn);
	$('html body').append(html);
	//把默认时间填充到input
	start_time = getValidDate(1);
	spl_start_time = start_time.split('-');
	$('.start_year').val(spl_start_time[0]);
	$('.start_month').val(spl_start_time[1]);
	$('.start_day').val(spl_start_time[2]);
	
	end_time = getValidDate(0);
	spl_end_time = end_time.split('-');
	$('.end_year').val(spl_end_time[0]);
	$('.end_month').val(spl_end_time[1]);
	$('.end_day').val(spl_end_time[2]);
	
},1000);

/* 
选项卡切换
 */
$('html body').on('change','.mode',function () {
	mode = $(this).val();
	if(mode == 1){
		$('.sjjq').css('display','block')
		$('.psjq').css('display','none')
	}else{
		$('.sjjq').css('display','none')
		$('.psjq').css('display','block')
	}
})

/* 
头条列表采集 
 */
$('html body').on('click','.userHomeBtn',function () {
	that =  $('.tt_collect_process');
	home_url = window.location.href;
	if(home_url.indexOf('c/user/token') != -1){
		wordsPrepend(that,'请点击作者头像再尝试！');
		return;
	}
	user_id = home_url.split('/')[5];
	content_objs = [];
	content_ids = [];
	max_behot_time = 0;
	mode = $("input[name='mode']:checked").val();
	wordsPrepend(that,'开始。。');
	//按照时间取
	if(mode == 1){
		//获取开始
		start_year = $('.start_year').val();
		start_month = $('.start_month').val();
		start_day = $('.start_day').val();
		
		end_year = $('.end_year').val();
		end_month = $('.end_month').val();
		end_day = $('.end_day').val();
		if(!start_day || !end_day){
			wordsPrepend(that,'请输入完整日期');
			return;
		}
		start_time = tranStrToTimestamps(start_year + '-' + start_month + '-' + start_day + " 00:00:00");
		end_time = tranStrToTimestamps(end_year + '-' + end_month + '-' + end_day + " 23:59:59");
		//请求item_id
		for(let i = 0;i<100;i++){
			tmp_has_more = false;
			data = homeList(user_id,max_behot_time);
			if(data){
				content_objs = content_objs.concat(data.article_list);
				tmp_has_more = data.hasMore;
				max_behot_time = data.max_behot_time;
				console.log(tmp_has_more);
				console.log(max_behot_time);
			}
			if(!tmp_has_more){
				break;
			}
		}
		//筛出需要的content_ids
		if(content_objs.length > 0){
			for(content_obj of content_objs){
				if(content_obj.pub_time > start_time && content_obj.pub_time < end_time){
					content_ids.push(content_obj.item_id);
				}
			}
		}
		
	}
	
	
	//按照篇章取
	if(mode == 2){
		//获取input框
		start_index = $(".start_index").val()
		end_index = $(".end_index").val()
		if(!start_index || !end_index){
			wordsPrepend(that,'请输入开始和结束篇章');
			return;
		}
		
		
		//请求item_id
		for(let i = 0;i<100;i++){
			tmp_has_more = false;
			data = homeList(user_id,max_behot_time);
			if(data){
				content_objs = content_objs.concat(data.article_list);
				tmp_has_more = data.hasMore;
				max_behot_time = data.max_behot_time;
				console.log(tmp_has_more);
				console.log(max_behot_time);
			}
			if(!(content_objs.length <  end_index && tmp_has_more)){
				break;
			}
		}
		//筛出需要的content_ids
		if(content_objs.length > 0){
			for(let i = start_index-1;i<end_index;i++){
				if(content_objs[i] == 'undefined'){
					break;
				}
				content_ids.push(content_objs[i].item_id);
			}
		}
	}

	console.log(content_ids);
	if(content_ids.length == 0){
		wordsPrepend(that,'无数据',1);
		return;
	}
	
	//转发流程
	begin = 0;
	intt = setInterval(function(){

		wordsPrepend(that,content_ids[begin],1);
		window.open('https://www.toutiao.com/i' + content_ids[begin]);
		console.log(content_ids[begin]);
		if((begin+1) >= content_ids.length){
			wordsPrepend(that,'搬运完成',1);
			clearInterval(intt);
			return;
		}
		begin++;
	},9000);
	
});


/* 
头条搜索主页id 
 */
function homeList(user_id,max_behot_time = 0){
	let url = 'https://www.toutiao.com/c/user/article/';
	
	result = {};
	$.ajax({
		url:url,
		type:"get",
		async:false,
		data:{
			page_type:1,
			user_id:user_id,
			max_behot_time:max_behot_time,
			count:20
		},
		success:function(data){
			if(data.message == 'success'){
				result.article_list = [];
				for(item of data.data){
					if(item.article_genre == 'article'){
						result.article_list.push({
							'item_id':item.item_id,
							'pub_time':item.behot_time
						});
					}
				}
				result.hasMore = data.has_more;
				result.max_behot_time = data.next.max_behot_time;
			}
		},
		error:function(){
			layer.msg('用户文章获取失败');
		}
	})
	return result;
}


